home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 October / PCgo 2008-10 (DVD).iso / interface / contents / vollversionen_6617 / 21733 / files / xulrunner / components / nsBlocklistService.js < prev    next >
Encoding:
Text File  |  2008-08-20  |  26.8 KB  |  812 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3. //@line 40 "e:\builds\tinderbox\XR-Trunk\WINNT_5.2_Depend\mozilla\toolkit\mozapps\extensions\src\nsBlocklistService.js"
  4. */
  5.  
  6. const Cc = Components.classes;
  7. const Ci = Components.interfaces;
  8. const Cr = Components.results;
  9.  
  10. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  11.  
  12. const TOOLKIT_ID                      = "toolkit@mozilla.org"
  13. const KEY_PROFILEDIR                  = "ProfD";
  14. const KEY_APPDIR                      = "XCurProcD";
  15. const FILE_BLOCKLIST                  = "blocklist.xml";
  16. const PREF_BLOCKLIST_URL              = "extensions.blocklist.url";
  17. const PREF_BLOCKLIST_ENABLED          = "extensions.blocklist.enabled";
  18. const PREF_BLOCKLIST_INTERVAL         = "extensions.blocklist.interval";
  19. const PREF_GENERAL_USERAGENT_LOCALE   = "general.useragent.locale";
  20. const PREF_PARTNER_BRANCH             = "app.partner.";
  21. const PREF_APP_DISTRIBUTION           = "distribution.id";
  22. const PREF_APP_DISTRIBUTION_VERSION   = "distribution.version";
  23. const PREF_APP_UPDATE_CHANNEL         = "app.update.channel";
  24. const PREF_EM_LOGGING_ENABLED         = "extensions.logging.enabled";
  25. const XMLURI_BLOCKLIST                = "http://www.mozilla.org/2006/addons-blocklist";
  26. const XMLURI_PARSE_ERROR              = "http://www.mozilla.org/newlayout/xml/parsererror.xml"
  27. const UNKNOWN_XPCOM_ABI               = "unknownABI";
  28.  
  29. const MODE_RDONLY   = 0x01;
  30. const MODE_WRONLY   = 0x02;
  31. const MODE_CREATE   = 0x08;
  32. const MODE_APPEND   = 0x10;
  33. const MODE_TRUNCATE = 0x20;
  34.  
  35. const PERMS_FILE      = 0644;
  36. const PERMS_DIRECTORY = 0755;
  37.  
  38. var gApp = null;
  39. var gPref = null;
  40. var gOS = null;
  41. var gConsole = null;
  42. var gVersionChecker = null;
  43. var gLoggingEnabled = null;
  44. var gABI = null;
  45. var gOSVersion = null;
  46.  
  47. // shared code for suppressing bad cert dialogs
  48. //@line 40 "e:\builds\tinderbox\XR-Trunk\WINNT_5.2_Depend\mozilla\toolkit\mozapps\shared\src\badCertHandler.js"
  49.  
  50. /**
  51.  * Only allow built-in certs for HTTPS connections.  See bug 340198.
  52.  */
  53. function checkCert(channel) {
  54.   if (!channel.originalURI.schemeIs("https"))  // bypass
  55.     return;
  56.  
  57.   const Ci = Components.interfaces;  
  58.   var cert =
  59.       channel.securityInfo.QueryInterface(Ci.nsISSLStatusProvider).
  60.       SSLStatus.QueryInterface(Ci.nsISSLStatus).serverCert;
  61.  
  62.   var issuer = cert.issuer;
  63.   while (issuer && !cert.equals(issuer)) {
  64.     cert = issuer;
  65.     issuer = cert.issuer;
  66.   }
  67.  
  68.   if (!issuer || issuer.tokenName != "Builtin Object Token")
  69.     throw "cert issuer is not built-in";
  70. }
  71.  
  72. /**
  73.  * This class implements nsIBadCertListener.  It's job is to prevent "bad cert"
  74.  * security dialogs from being shown to the user.  It is better to simply fail
  75.  * if the certificate is bad. See bug 304286.
  76.  */
  77. function BadCertHandler() {
  78. }
  79. BadCertHandler.prototype = {
  80.  
  81.   // nsIChannelEventSink
  82.   onChannelRedirect: function(oldChannel, newChannel, flags) {
  83.     // make sure the certificate of the old channel checks out before we follow
  84.     // a redirect from it.  See bug 340198.
  85.     checkCert(oldChannel);
  86.   },
  87.  
  88.   // Suppress any certificate errors
  89.   notifyCertProblem: function(socketInfo, status, targetSite) {
  90.     return true;
  91.   },
  92.  
  93.   // Suppress any ssl errors
  94.   notifySSLError: function(socketInfo, error, targetSite) {
  95.     return true;
  96.   },
  97.  
  98.   // nsIInterfaceRequestor
  99.   getInterface: function(iid) {
  100.     return this.QueryInterface(iid);
  101.   },
  102.  
  103.   // nsISupports
  104.   QueryInterface: function(iid) {
  105.     if (!iid.equals(Components.interfaces.nsIChannelEventSink) &&
  106.         !iid.equals(Components.interfaces.nsIBadCertListener2) &&
  107.         !iid.equals(Components.interfaces.nsISSLErrorListener) &&
  108.         !iid.equals(Components.interfaces.nsIInterfaceRequestor) &&
  109.         !iid.equals(Components.interfaces.nsISupports))
  110.       throw Components.results.NS_ERROR_NO_INTERFACE;
  111.     return this;
  112.   }
  113. };
  114. //@line 85 "e:\builds\tinderbox\XR-Trunk\WINNT_5.2_Depend\mozilla\toolkit\mozapps\extensions\src\nsBlocklistService.js"
  115.  
  116. /**
  117.  * Logs a string to the error console.
  118.  * @param   string
  119.  *          The string to write to the error console..
  120.  */
  121. function LOG(string) {
  122.   if (gLoggingEnabled) {
  123.     dump("*** " + string + "\n");
  124.     if (gConsole)
  125.       gConsole.logStringMessage(string);
  126.   }
  127. }
  128.  
  129. /**
  130.  * Gets a preference value, handling the case where there is no default.
  131.  * @param   func
  132.  *          The name of the preference function to call, on nsIPrefBranch
  133.  * @param   preference
  134.  *          The name of the preference
  135.  * @param   defaultValue
  136.  *          The default value to return in the event the preference has
  137.  *          no setting
  138.  * @returns The value of the preference, or undefined if there was no
  139.  *          user or default value.
  140.  */
  141. function getPref(func, preference, defaultValue) {
  142.   try {
  143.     return gPref[func](preference);
  144.   }
  145.   catch (e) {
  146.   }
  147.   return defaultValue;
  148. }
  149.  
  150. /**
  151.  * Gets the file at the specified hierarchy under a Directory Service key.
  152.  * @param   key
  153.  *          The Directory Service Key to start from
  154.  * @param   pathArray
  155.  *          An array of path components to locate beneath the directory
  156.  *          specified by |key|. The last item in this array must be the
  157.  *          leaf name of a file.
  158.  * @return  nsIFile object for the file specified. The file is NOT created
  159.  *          if it does not exist, however all required directories along
  160.  *          the way are.
  161.  */
  162. function getFile(key, pathArray) {
  163.   var fileLocator = Cc["@mozilla.org/file/directory_service;1"].
  164.                     getService(Ci.nsIProperties);
  165.   var file = fileLocator.get(key, Ci.nsILocalFile);
  166.   for (var i = 0; i < pathArray.length - 1; ++i) {
  167.     file.append(pathArray[i]);
  168.     if (!file.exists())
  169.       file.create(Ci.nsILocalFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
  170.   }
  171.   file.followLinks = false;
  172.   file.append(pathArray[pathArray.length - 1]);
  173.   return file;
  174. }
  175.  
  176. /**
  177.  * Opens a safe file output stream for writing.
  178.  * @param   file
  179.  *          The file to write to.
  180.  * @param   modeFlags
  181.  *          (optional) File open flags. Can be undefined.
  182.  * @returns nsIFileOutputStream to write to.
  183.  */
  184. function openSafeFileOutputStream(file, modeFlags) {
  185.   var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
  186.             createInstance(Ci.nsIFileOutputStream);
  187.   if (modeFlags === undefined)
  188.     modeFlags = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE;
  189.   if (!file.exists())
  190.     file.create(Ci.nsILocalFile.NORMAL_FILE_TYPE, PERMS_FILE);
  191.   fos.init(file, modeFlags, PERMS_FILE, 0);
  192.   return fos;
  193. }
  194.  
  195. /**
  196.  * Closes a safe file output stream.
  197.  * @param   stream
  198.  *          The stream to close.
  199.  */
  200. function closeSafeFileOutputStream(stream) {
  201.   if (stream instanceof Ci.nsISafeOutputStream)
  202.     stream.finish();
  203.   else
  204.     stream.close();
  205. }
  206.  
  207. /**
  208.  * Constructs a URI to a spec.
  209.  * @param   spec
  210.  *          The spec to construct a URI to
  211.  * @returns The nsIURI constructed.
  212.  */
  213. function newURI(spec) {
  214.   var ioServ = Cc["@mozilla.org/network/io-service;1"].
  215.                getService(Ci.nsIIOService);
  216.   return ioServ.newURI(spec, null, null);
  217. }
  218.  
  219. /**
  220.  * Checks whether this blocklist element is valid for the current OS and ABI.
  221.  * If the element has an "os" attribute then the current OS must appear in
  222.  * it's comma separated list for the element to be valid. Similarly for the
  223.  * xpcomabi attribute.
  224.  */
  225. function matchesOSABI(blocklistElement) {
  226.   if (blocklistElement.hasAttribute("os")) {
  227.     var choices = blocklistElement.getAttribute("os").split(",");
  228.     if (choices.length > 0 && choices.indexOf(gApp.OS) < 0)
  229.       return false;
  230.   }
  231.   
  232.   if (blocklistElement.hasAttribute("xpcomabi")) {
  233.     choices = blocklistElement.getAttribute("xpcomabi").split(",");
  234.     if (choices.length > 0 && choices.indexOf(gApp.XPCOMABI) < 0)
  235.       return false;
  236.   }
  237.   
  238.   return true;
  239. }
  240.  
  241. /**
  242.  * Gets the current value of the locale.  It's possible for this preference to
  243.  * be localized, so we have to do a little extra work here.  Similar code
  244.  * exists in nsHttpHandler.cpp when building the UA string.
  245.  */
  246. function getLocale() {
  247.   try {
  248.       // Get the default branch
  249.       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  250.           .getService(Components.interfaces.nsIPrefService);
  251.       var defaultPrefs = prefs.getDefaultBranch(null);
  252.       return defaultPrefs.getCharPref(PREF_GENERAL_USERAGENT_LOCALE);
  253.   } catch (e) {}
  254.  
  255.   return gPref.getCharPref(PREF_GENERAL_USERAGENT_LOCALE);
  256. }
  257.  
  258. /**
  259.  * Read the update channel from defaults only.  We do this to ensure that
  260.  * the channel is tightly coupled with the application and does not apply
  261.  * to other installations of the application that may use the same profile.
  262.  */
  263. function getUpdateChannel() {
  264.   var channel = "default";
  265.   var prefName;
  266.   var prefValue;
  267.  
  268.   var defaults =
  269.       gPref.QueryInterface(Components.interfaces.nsIPrefService).
  270.       getDefaultBranch(null);
  271.   try {
  272.     channel = defaults.getCharPref(PREF_APP_UPDATE_CHANNEL);
  273.   } catch (e) {
  274.     // use default when pref not found
  275.   }
  276.  
  277.   try {
  278.     var partners = gPref.getChildList(PREF_PARTNER_BRANCH, { });
  279.     if (partners.length) {
  280.       channel += "-cck";
  281.       partners.sort();
  282.  
  283.       for each (prefName in partners) {
  284.         prefValue = gPref.getCharPref(prefName);
  285.         channel += "-" + prefValue;
  286.       }
  287.     }
  288.   }
  289.   catch (e) {
  290.     Components.utils.reportError(e);
  291.   }
  292.  
  293.   return channel;
  294. }
  295.  
  296. /* Get the distribution pref values, from defaults only */
  297. function getDistributionPrefValue(aPrefName) {
  298.   var prefValue = "default";
  299.  
  300.   var defaults =
  301.       gPref.QueryInterface(Components.interfaces.nsIPrefService).
  302.       getDefaultBranch(null);
  303.   try {
  304.     prefValue = defaults.getCharPref(aPrefName);
  305.   } catch (e) {
  306.     // use default when pref not found
  307.   }
  308.  
  309.   return prefValue;
  310. }
  311.  
  312. /**
  313.  * Manages the Blocklist. The Blocklist is a representation of the contents of
  314.  * blocklist.xml and allows us to remotely disable / re-enable blocklisted
  315.  * items managed by the Extension Manager with an item's appDisabled property.
  316.  * It also blocklists plugins with data from blocklist.xml.
  317.  */
  318.  
  319. function Blocklist() {
  320.   gApp = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
  321.   gApp.QueryInterface(Ci.nsIXULRuntime);
  322.   gPref = Cc["@mozilla.org/preferences-service;1"].
  323.           getService(Ci.nsIPrefBranch2);
  324.   gVersionChecker = Cc["@mozilla.org/xpcom/version-comparator;1"].
  325.                     getService(Ci.nsIVersionComparator);
  326.   gConsole = Cc["@mozilla.org/consoleservice;1"].
  327.              getService(Ci.nsIConsoleService);
  328.  
  329.   gOS = Cc["@mozilla.org/observer-service;1"].
  330.         getService(Ci.nsIObserverService);
  331.   gOS.addObserver(this, "xpcom-shutdown", false);
  332.  
  333.   // Not all builds have a known ABI
  334.   try {
  335.     gABI = gApp.XPCOMABI;
  336.   }
  337.   catch (e) {
  338.     LOG("Blocklist: XPCOM ABI unknown.");
  339.     gABI = UNKNOWN_XPCOM_ABI;
  340.   }
  341.  
  342.   var osVersion;
  343.   var sysInfo = Components.classes["@mozilla.org/system-info;1"]
  344.                           .getService(Components.interfaces.nsIPropertyBag2);
  345.   try {
  346.     osVersion = sysInfo.getProperty("name") + " " + sysInfo.getProperty("version");
  347.   }
  348.   catch (e) {
  349.     LOG("Blocklist: OS Version unknown.");
  350.   }
  351.  
  352.   if (osVersion) {
  353.     try {
  354.       osVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
  355.     }
  356.     catch (e) {
  357.       // Not all platforms have a secondary widget library, so an error is nothing to worry about.
  358.     }
  359.     gOSVersion = encodeURIComponent(osVersion);
  360.   }
  361.  
  362. //@line 341 "e:\builds\tinderbox\XR-Trunk\WINNT_5.2_Depend\mozilla\toolkit\mozapps\extensions\src\nsBlocklistService.js"
  363. }
  364.  
  365. Blocklist.prototype = {
  366.   /**
  367.    * Extension ID -> array of Version Ranges
  368.    * Each value in the version range array is a JS Object that has the
  369.    * following properties:
  370.    *   "minVersion"  The minimum version in a version range (default = 0)
  371.    *   "maxVersion"  The maximum version in a version range (default = *)
  372.    *   "targetApps"  Application ID -> array of Version Ranges
  373.    *                 (default = current application ID)
  374.    *                 Each value in the version range array is a JS Object that
  375.    *                 has the following properties:
  376.    *                   "minVersion"  The minimum version in a version range
  377.    *                                 (default = 0)
  378.    *                   "maxVersion"  The maximum version in a version range
  379.    *                                 (default = *)
  380.    */
  381.   _addonEntries: null,
  382.   _pluginEntries: null,
  383.  
  384.   observe: function (aSubject, aTopic, aData) {
  385.     switch (aTopic) {
  386.     case "app-startup":
  387.       gOS.addObserver(this, "plugins-list-updated", false);
  388.       gOS.addObserver(this, "profile-after-change", false);
  389.       gOS.addObserver(this, "quit-application", false);
  390.       break;
  391.     case "profile-after-change":
  392.       gLoggingEnabled = getPref("getBoolPref", PREF_EM_LOGGING_ENABLED, false);
  393.       var tm = Cc["@mozilla.org/updates/timer-manager;1"].
  394.                getService(Ci.nsIUpdateTimerManager);
  395.       var interval = getPref("getIntPref", PREF_BLOCKLIST_INTERVAL, 86400);
  396.       tm.registerTimer("blocklist-background-update-timer", this, interval);
  397.       break;
  398.     case "plugins-list-updated":
  399.       this._checkPluginsList();
  400.       break;
  401.     case "quit-application":
  402.       gOS.removeObserver(this, "plugins-list-updated");
  403.       gOS.removeObserver(this, "profile-after-change");
  404.       gOS.removeObserver(this, "quit-application");
  405.       break;
  406.     case "xpcom-shutdown":
  407.       gOS.removeObserver(this, "xpcom-shutdown");
  408.       gOS = null;
  409.       gPref = null;
  410.       gConsole = null;
  411.       gVersionChecker = null;
  412.       gApp = null;
  413.       break;
  414.     }
  415.   },
  416.  
  417.   isAddonBlocklisted: function(id, version, appVersion, toolkitVersion) {
  418.     if (!this._addonEntries)
  419.       this._loadBlocklist();
  420.     if (!appVersion)
  421.       appVersion = gApp.version;
  422.     if (!toolkitVersion)
  423.       toolkitVersion = gApp.platformVersion;
  424.  
  425.     var blItem = this._addonEntries[id];
  426.     if (!blItem)
  427.       return false;
  428.  
  429.     for (var i = 0; i < blItem.length; ++i) {
  430.       if (gVersionChecker.compare(version, blItem[i].minVersion) < 0  ||
  431.           gVersionChecker.compare(version, blItem[i].maxVersion) > 0)
  432.         continue;
  433.  
  434.       var blTargetApp = blItem[i].targetApps[gApp.ID];
  435.       if (blTargetApp) {
  436.         for (var x = 0; x < blTargetApp.length; ++x) {
  437.           if (gVersionChecker.compare(appVersion, blTargetApp[x].minVersion) < 0 ||
  438.               gVersionChecker.compare(appVersion, blTargetApp[x].maxVersion) > 0)
  439.             continue;
  440.           return true;
  441.         }
  442.       }
  443.  
  444.       blTargetApp = blItem[i].targetApps[TOOLKIT_ID];
  445.       if (!blTargetApp)
  446.         return false;
  447.       for (x = 0; x < blTargetApp.length; ++x) {
  448.         if (gVersionChecker.compare(toolkitVersion, blTargetApp[x].minVersion) < 0 ||
  449.             gVersionChecker.compare(toolkitVersion, blTargetApp[x].maxVersion) > 0)
  450.           continue;
  451.         return true;
  452.       }
  453.     }
  454.     return false;
  455.   },
  456.  
  457.   notify: function(aTimer) {
  458.     if (getPref("getBoolPref", PREF_BLOCKLIST_ENABLED, true) == false)
  459.       return;
  460.  
  461.     try {
  462.       var dsURI = gPref.getCharPref(PREF_BLOCKLIST_URL);
  463.     }
  464.     catch (e) {
  465.       LOG("Blocklist::notify: The " + PREF_BLOCKLIST_URL + " preference" +
  466.           " is missing!");
  467.       return;
  468.     }
  469.  
  470.     dsURI = dsURI.replace(/%APP_ID%/g, gApp.ID);
  471.     dsURI = dsURI.replace(/%APP_VERSION%/g, gApp.version);
  472.     dsURI = dsURI.replace(/%PRODUCT%/g, gApp.name);
  473.     dsURI = dsURI.replace(/%VERSION%/g, gApp.version);
  474.     dsURI = dsURI.replace(/%BUILD_ID%/g, gApp.appBuildID);
  475.     dsURI = dsURI.replace(/%BUILD_TARGET%/g, gApp.OS + "_" + gABI);
  476.     dsURI = dsURI.replace(/%OS_VERSION%/g, gOSVersion);
  477.     dsURI = dsURI.replace(/%LOCALE%/g, getLocale());
  478.     dsURI = dsURI.replace(/%CHANNEL%/g, getUpdateChannel());
  479.     dsURI = dsURI.replace(/%PLATFORM_VERSION%/g, gApp.platformVersion);
  480.     dsURI = dsURI.replace(/%DISTRIBUTION%/g,
  481.                       getDistributionPrefValue(PREF_APP_DISTRIBUTION));
  482.     dsURI = dsURI.replace(/%DISTRIBUTION_VERSION%/g,
  483.                       getDistributionPrefValue(PREF_APP_DISTRIBUTION_VERSION));
  484.     dsURI = dsURI.replace(/\+/g, "%2B");
  485.  
  486.     // Verify that the URI is valid
  487.     try {
  488.       var uri = newURI(dsURI);
  489.     }
  490.     catch (e) {
  491.       LOG("Blocklist::notify: There was an error creating the blocklist URI\r\n" +
  492.           "for: " + dsURI + ", error: " + e);
  493.       return;
  494.     }
  495.  
  496.     var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
  497.                   createInstance(Ci.nsIXMLHttpRequest);
  498.     request.open("GET", uri.spec, true);
  499.     request.channel.notificationCallbacks = new BadCertHandler();
  500.     request.overrideMimeType("text/xml");
  501.     request.setRequestHeader("Cache-Control", "no-cache");
  502.     request.QueryInterface(Components.interfaces.nsIJSXMLHttpRequest);
  503.  
  504.     var self = this;
  505.     request.onerror = function(event) { self.onXMLError(event); };
  506.     request.onload  = function(event) { self.onXMLLoad(event);  };
  507.     request.send(null);
  508.   },
  509.  
  510.   onXMLLoad: function(aEvent) {
  511.     var request = aEvent.target;
  512.     try {
  513.       checkCert(request.channel);
  514.     }
  515.     catch (e) {
  516.       LOG("Blocklist::onXMLLoad: " + e);
  517.       return;
  518.     }
  519.     var responseXML = request.responseXML;
  520.     if (!responseXML || responseXML.documentElement.namespaceURI == XMLURI_PARSE_ERROR ||
  521.         (request.status != 200 && request.status != 0)) {
  522.       LOG("Blocklist::onXMLLoad: there was an error during load");
  523.       return;
  524.     }
  525.     var blocklistFile = getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
  526.     if (blocklistFile.exists())
  527.       blocklistFile.remove(false);
  528.     var fos = openSafeFileOutputStream(blocklistFile);
  529.     fos.write(request.responseText, request.responseText.length);
  530.     closeSafeFileOutputStream(fos);
  531.     this._loadBlocklistFromFile(getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]));
  532.     var em = Cc["@mozilla.org/extensions/manager;1"].
  533.              getService(Ci.nsIExtensionManager);
  534.     em.checkForBlocklistChanges();
  535.     this._checkPluginsList();
  536.   },
  537.  
  538.   onXMLError: function(aEvent) {
  539.     try {
  540.       var request = aEvent.target;
  541.       // the following may throw (e.g. a local file or timeout)
  542.       var status = request.status;
  543.     }
  544.     catch (e) {
  545.       request = aEvent.target.channel.QueryInterface(Ci.nsIRequest);
  546.       status = request.status;
  547.     }
  548.     var statusText = request.statusText;
  549.     // When status is 0 we don't have a valid channel.
  550.     if (status == 0)
  551.       statusText = "nsIXMLHttpRequest channel unavailable";
  552.     LOG("Blocklist:onError: There was an error loading the blocklist file\r\n" +
  553.         statusText);
  554.   },
  555.  
  556.   /**
  557.    * Finds the newest blocklist file from the application and the profile and
  558.    * load it or does nothing if neither exist.
  559.    */
  560.   _loadBlocklist: function() {
  561.     this._addonEntries = { };
  562.     this._pluginEntries = { };
  563.     var profFile = getFile(KEY_PROFILEDIR, [FILE_BLOCKLIST]);
  564.     if (profFile.exists()) {
  565.       this._loadBlocklistFromFile(profFile);
  566.       return;
  567.     }
  568.     var appFile = getFile(KEY_APPDIR, [FILE_BLOCKLIST]);
  569.     if (appFile.exists()) {
  570.       this._loadBlocklistFromFile(appFile);
  571.       return;
  572.     }
  573.     LOG("Blocklist::_loadBlocklist: no XML File found");
  574.   },
  575.  
  576.   /**
  577. //@line 604 "e:\builds\tinderbox\XR-Trunk\WINNT_5.2_Depend\mozilla\toolkit\mozapps\extensions\src\nsBlocklistService.js"
  578.    */
  579.  
  580.   _loadBlocklistFromFile: function(file) {
  581.     if (getPref("getBoolPref", PREF_BLOCKLIST_ENABLED, true) == false) {
  582.       LOG("Blocklist::_loadBlocklistFromFile: blocklist is disabled");
  583.       return;
  584.     }
  585.  
  586.     if (!file.exists()) {
  587.       LOG("Blocklist::_loadBlocklistFromFile: XML File does not exist");
  588.       return;
  589.     }
  590.  
  591.     var fileStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
  592.                                .createInstance(Components.interfaces.nsIFileInputStream);
  593.     fileStream.init(file, MODE_RDONLY, PERMS_FILE, 0);
  594.     try {
  595.       var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
  596.                    createInstance(Ci.nsIDOMParser);
  597.       var doc = parser.parseFromStream(fileStream, "UTF-8", file.fileSize, "text/xml");
  598.       if (doc.documentElement.namespaceURI != XMLURI_BLOCKLIST) {
  599.         LOG("Blocklist::_loadBlocklistFromFile: aborting due to incorrect " +
  600.             "XML Namespace.\r\nExpected: " + XMLURI_BLOCKLIST + "\r\n" +
  601.             "Received: " + doc.documentElement.namespaceURI);
  602.         return;
  603.       }
  604.  
  605.       var childNodes = doc.documentElement.childNodes;
  606.       this._addonEntries = this._processItemNodes(childNodes, "em",
  607.                                             this._handleEmItemNode);
  608.       this._pluginEntries = this._processItemNodes(childNodes, "plugin",
  609.                                                    this._handlePluginItemNode);
  610.     }
  611.     catch (e) {
  612.       LOG("Blocklist::_loadBlocklistFromFile: Error constructing blocklist " + e);
  613.       return;
  614.     }
  615.     fileStream.close();
  616.   },
  617.  
  618.   _processItemNodes: function(deChildNodes, prefix, handler) {
  619.     var result = [];
  620.     var itemNodes;
  621.     var containerName = prefix + "Items";
  622.     for (var i = 0; i < deChildNodes.length; ++i) {
  623.       var emItemsElement = deChildNodes.item(i);
  624.       if (emItemsElement instanceof Ci.nsIDOMElement &&
  625.           emItemsElement.localName == containerName) {
  626.         itemNodes = emItemsElement.childNodes;
  627.         break;
  628.       }
  629.     }
  630.     if (!itemNodes)
  631.       return result;
  632.  
  633.     var itemName = prefix + "Item";
  634.     for (var i = 0; i < itemNodes.length; ++i) {
  635.       var blocklistElement = itemNodes.item(i);
  636.       if (!(blocklistElement instanceof Ci.nsIDOMElement) ||
  637.           blocklistElement.localName != itemName)
  638.         continue;
  639.  
  640.       handler(blocklistElement, result);
  641.     }
  642.     return result;
  643.   },
  644.  
  645.   _handleEmItemNode: function(blocklistElement, result) {
  646.     if (!matchesOSABI(blocklistElement))
  647.       return;
  648.  
  649.     var versionNodes = blocklistElement.childNodes;
  650.     var id = blocklistElement.getAttribute("id");
  651.     result[id] = [];
  652.     for (var x = 0; x < versionNodes.length; ++x) {
  653.       var versionRangeElement = versionNodes.item(x);
  654.       if (!(versionRangeElement instanceof Ci.nsIDOMElement) ||
  655.           versionRangeElement.localName != "versionRange")
  656.         continue;
  657.  
  658.       result[id].push(new BlocklistItemData(versionRangeElement));
  659.     }
  660.     // if only the extension ID is specified block all versions of the
  661.     // extension for the current application.
  662.     if (result[id].length == 0)
  663.       result[id].push(new BlocklistItemData(null));
  664.   },
  665.  
  666.   _handlePluginItemNode: function(blocklistElement, result) {
  667.     if (!matchesOSABI(blocklistElement))
  668.       return;
  669.  
  670.     var matchNodes = blocklistElement.childNodes;
  671.     var matchList;
  672.     for (var x = 0; x < matchNodes.length; ++x) {
  673.       var matchElement = matchNodes.item(x);
  674.       if (!(matchElement instanceof Ci.nsIDOMElement) ||
  675.           matchElement.localName != "match")
  676.         continue;
  677.  
  678.       var name = matchElement.getAttribute("name");
  679.       var exp = matchElement.getAttribute("exp");
  680.       if (!matchList)
  681.         matchList = { };
  682.       matchList[name] = new RegExp(exp, "m");
  683.     }
  684.     if (matchList)
  685.       result.push(matchList);
  686.   },
  687.  
  688.   _checkPlugin: function(plugin) {
  689.     for each (var matchList in this._pluginEntries) {
  690.       var matchFailed = false;
  691.       for (var name in matchList) {
  692.         if (typeof(plugin[name]) != "string" ||
  693.             !matchList[name].test(plugin[name])) {
  694.           matchFailed = true;
  695.           break;
  696.         }
  697.       }
  698.  
  699.       if (!matchFailed) {
  700.         plugin.blocklisted = true;
  701.         return;
  702.       }
  703.     }
  704.     plugin.blocklisted = false;
  705.   },
  706.  
  707.   _checkPluginsList: function() {
  708.     if (!this._addonEntries)
  709.       this._loadBlocklist();
  710.     var phs = Cc["@mozilla.org/plugin/host;1"].
  711.               getService(Ci.nsIPluginHost);
  712.     phs.getPluginTags({ }).forEach(this._checkPlugin, this);
  713.   },
  714.  
  715.   classDescription: "Blocklist Service",
  716.   contractID: "@mozilla.org/extensions/blocklist;1",
  717.   classID: Components.ID("{66354bc9-7ed1-4692-ae1d-8da97d6b205e}"),
  718.   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
  719.                                          Ci.nsIBlocklistService,
  720.                                          Ci.nsITimerCallback]),
  721.   _xpcom_categories: [{
  722.     category: "app-startup",
  723.     service: true
  724.   }]
  725. };
  726.  
  727. /**
  728.  * Helper for constructing a blocklist.
  729.  */
  730. function BlocklistItemData(versionRangeElement) {
  731.   var versionRange = this.getBlocklistVersionRange(versionRangeElement);
  732.   this.minVersion = versionRange.minVersion;
  733.   this.maxVersion = versionRange.maxVersion;
  734.   this.targetApps = { };
  735.   var found = false;
  736.  
  737.   if (versionRangeElement) {
  738.     for (var i = 0; i < versionRangeElement.childNodes.length; ++i) {
  739.       var targetAppElement = versionRangeElement.childNodes.item(i);
  740.       if (!(targetAppElement instanceof Ci.nsIDOMElement) ||
  741.           targetAppElement.localName != "targetApplication")
  742.         continue;
  743.       found = true;
  744.       // default to the current application if id is not provided.
  745.       var appID = targetAppElement.hasAttribute("id") ? targetAppElement.getAttribute("id") : gApp.ID;
  746.       this.targetApps[appID] = this.getBlocklistAppVersions(targetAppElement);
  747.     }
  748.   }
  749.   // Default to all versions of the extension and the current application when
  750.   // versionRange is not defined.
  751.   if (!found)
  752.     this.targetApps[gApp.ID] = this.getBlocklistAppVersions(null);
  753. }
  754.  
  755. BlocklistItemData.prototype = {
  756. /**
  757.  * Retrieves a version range (e.g. minVersion and maxVersion) for a
  758.  * blocklist item's targetApplication element.
  759.  * @param   targetAppElement
  760.  *          A targetApplication blocklist element.
  761.  * @returns An array of JS objects with the following properties:
  762.  *          "minVersion"  The minimum version in a version range (default = 0).
  763.  *          "maxVersion"  The maximum version in a version range (default = *).
  764.  */
  765.   getBlocklistAppVersions: function(targetAppElement) {
  766.     var appVersions = [ ];
  767.     var found = false;
  768.  
  769.     if (targetAppElement) {
  770.       for (var i = 0; i < targetAppElement.childNodes.length; ++i) {
  771.         var versionRangeElement = targetAppElement.childNodes.item(i);
  772.         if (!(versionRangeElement instanceof Ci.nsIDOMElement) ||
  773.             versionRangeElement.localName != "versionRange")
  774.           continue;
  775.         found = true;
  776.         appVersions.push(this.getBlocklistVersionRange(versionRangeElement));
  777.       }
  778.     }
  779.     // return minVersion = 0 and maxVersion = * if not available
  780.     if (!found)
  781.       return [ this.getBlocklistVersionRange(null) ];
  782.     return appVersions;
  783.   },
  784.  
  785. /**
  786.  * Retrieves a version range (e.g. minVersion and maxVersion) for a blocklist
  787.  * versionRange element.
  788.  * @param   versionRangeElement
  789.  *          The versionRange blocklist element.
  790.  * @returns A JS object with the following properties:
  791.  *          "minVersion"  The minimum version in a version range (default = 0).
  792.  *          "maxVersion"  The maximum version in a version range (default = *).
  793.  */
  794.   getBlocklistVersionRange: function(versionRangeElement) {
  795.     var minVersion = "0";
  796.     var maxVersion = "*";
  797.     if (!versionRangeElement)
  798.       return { minVersion: minVersion, maxVersion: maxVersion };
  799.  
  800.     if (versionRangeElement.hasAttribute("minVersion"))
  801.       minVersion = versionRangeElement.getAttribute("minVersion");
  802.     if (versionRangeElement.hasAttribute("maxVersion"))
  803.       maxVersion = versionRangeElement.getAttribute("maxVersion");
  804.  
  805.     return { minVersion: minVersion, maxVersion: maxVersion };
  806.   }
  807. };
  808.  
  809. function NSGetModule(aCompMgr, aFileSpec) {
  810.   return XPCOMUtils.generateModule([Blocklist]);
  811. }
  812.